package com.study.project.controller;

import com.study.project.entity.Employee;
import com.study.project.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/save")
    public Object save(){
        Employee employee = getEmployee();
        Employee emp = employeeService.save(employee);
        System.out.println("保存成功" + emp);
        return emp;
    }

    @RequestMapping("/delete")
    public Object delete(){
        employeeService.delete("1");
        System.out.println("删除成功");
        return null;
    }

    @RequestMapping("/update")
    public Object update(){
        Employee employee = getEmployee();
        employeeService.update(employee);
        System.out.println("删除成功" + employee);
        return null;
    }

    @RequestMapping("/select")
    public Object select(){
        Employee select = employeeService.select("1");
        System.out.println("查询" + select);
        return null;
    }

    /*****json测试*******/
    @ResponseBody
    @RequestMapping("/select2")
    public Object select2(){
        Employee select = employeeService.select("805befa7aeba4c32a584984ce63db5f3");
        select.setUsername("不知道————————");
        System.out.println(select);
        return select;
    }

    @ResponseBody
    @RequestMapping("/save2")
    public Object save2(@RequestBody Employee employee){
        Employee emp = employeeService.save(employee);
        System.out.println("保存成功" + emp);
        return emp;
    }

    @ResponseBody
    @RequestMapping("/findAll")
    public Object findAll() {
        return employeeService.findAll();
    }

    //bak
    @RequestMapping("/")
    public Object method(){
        return null;
    }

    private Employee getEmployee() {
        Employee employee = new Employee();
        employee.setAddress("福建省福州市");
        employee.setCreateTime(new Date());
        employee.setState("A");
        employee.setUsername("小明2");
        employee.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        return employee;
    }

}